findEventDetail();
findMarketCount();

var page_count = 15;
var market_count_list;

/**
 * 查询比赛详情
 */
function findEventDetail() {

    $.ajax({
        url: baseUrl.base + '/country/eventInfo',
        type: 'get',
        data: {"eventId": baseFn.getQueryString('eventId')},
        success: function (data) {

            if (data.code == '0') {
                //创建成功

                $('#home_name_1').text(data.results.homeTeamName);
                $('#home_name_2').text(data.results.homeTeamName);

                $('#away_name_1').text(data.results.awayTeamName);
                $('#away_name_2').text(data.results.awayTeamName);

                var competitionName = data.results.competitionName;
                var teamName = data.results.homeTeamName + 'VS' + data.results.awayTeamName;
                var eventTime = data.results.eventTime / 10000;
                var score = data.results.homeTeamScore + ' : ' + data.results.awayTeamScore;
                var state = data.results.state;
                var stage = data.results.stage;

                var category = '';
                category += competitionName + "  ";
                category += teamName + ' ';

                var str = '';
                if (state == 3) {
                    if (stage == 2) {
                        str = "比赛正在上半场";
                    } else if (stage == 4) {
                        str = "比赛正在中场休息";
                    } else if (stage == 8) {
                        str = "比赛正在下半场";
                    } else if (stage == 16) {
                        str = "比赛全场结束";
                    } else if (stage == 32) {
                        str = "比赛常规时间结束";
                    } else if (stage == 64) {
                        str = "比赛正在加时赛上半场";
                    } else if (stage == 128) {
                        str = "比赛正在加时赛中场休息";
                    } else if (stage == 256) {
                        str = "比赛正在加时赛下半场";
                    } else if (stage == 512) {
                        str = "比赛加时赛结束";
                    } else if (stage == 1024) {
                        str = "比赛正在点球大战";
                    }
                }
                category += str;

                if (state != 0) {
                    category += '比分' + score;
                }

                $('#card-category').text(category);

                $('#card-title').text('开赛时间:' + timestampToTime(eventTime));
            }
        }
    })
}

/**
 * 查询统计
 */
function findMarketCount() {
    var eventId = baseFn.getQueryString('eventId');
    var ruleType = baseFn.getQueryString('ruleType');

    $.ajax({
        url: baseUrl.base + '/order/selectOrderCount',
        type: 'get',
        data: {"eventId":eventId,"ruleType":ruleType},
        success: function (data) {

            if (data.code == '0') {
                var res = data.results;

                market_count_list = res;

                cutPagination(1);

                // 配置分页
                $("#p1").pagination({
                    totalData: res.length,
                    showData:page_count,
                    callback: function (idx) {
                        cutPagination(idx);
                    }
                });
            }
        }
    })
}

/**
 * 跳转明细
 */
function gotoDetial(element) {
    var eventId = baseFn.getQueryString('eventId');
    var ruleType = baseFn.getQueryString('ruleType');
    var selectionId = element.getAttribute("selection_id");

    window.location.href = "market-detail.do?eventId=" + eventId + '&ruleType=' + ruleType + '&selectionId=' + selectionId;
}

/**
 * 分页
 * @param idx
 */
function cutPagination(page) {

    var start = (page - 1) * page_count;
    var end = start + page_count;

    if (end > market_count_list.length){
        end = market_count_list.length;
    }

    $("#tbody").html("");

    for (var i = start;i < end;i ++){
        var count = market_count_list[i];
        var selectName = count.selection_name;
        var totalMoney = count.totalMonty;
        var totalCount = count.totalCount;
        var selection_id = count.selection_id;

        var tr = document.createElement("tr");
        var td = document.createElement("td");
        var tdContent = document.createTextNode(selectName);
        td.appendChild(tdContent);
        tr.appendChild(td);

        var td = document.createElement("td");
        var tdContent = document.createTextNode(totalCount + '次');
        td.appendChild(tdContent);
        tr.appendChild(td);

        var td = document.createElement("td");
        var tdContent = document.createTextNode(totalMoney + 'KSH');
        td.appendChild(tdContent);
        tr.appendChild(td);

        var td = document.createElement("td");
        var btn1 = document.createElement("button");
        btn1.setAttribute("type", "button");
        btn1.setAttribute("class", "btn btn-success btn-sm");
        btn1.setAttribute("selection_id",selection_id);

        var tdContent = document.createTextNode("明细");
        btn1.appendChild(tdContent);
        btn1.onclick = function (ev) {
            gotoDetial(this);
        }

        td.appendChild(btn1);
        tr.appendChild(td);

        document.getElementById("tbody").append(tr);
        document.getElementById("tbody").innerHTML;
    }
}